package com.zrt.bookstore3.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrt.bookstore3.Dao.OrderDao;
import com.zrt.bookstore3.Dao.UserDao;
import com.zrt.bookstore3.Domain.Book;
import com.zrt.bookstore3.Domain.Order;
import com.zrt.bookstore3.Domain.User;
import com.zrt.bookstore3.Service.OrderService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao,Order>  implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Override
    public boolean updateOrder(Order order) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long times = System.currentTimeMillis();
        order.setPayTime(format.format(times));
        order.setStatus(1);
        return orderDao.updateOrder(order) > 0;
    }

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        orderDao.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<Order> getPage(int currentPage, int pageSize, Order order) {
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<Order>();


        IPage page = new Page(currentPage,pageSize);
        orderDao.selectPage(page,lqw);
        return page;
    }

    @Override
    public List<Order> selectByCondition(Order order) {
        return orderDao.selectByCondition(order);
    }

    @Override
    public boolean updateStatusAndNumber(Order order) {
        return orderDao.updateStatusAndNumber(order) > 0;
    }

    @Override
    public Order selectById(int id) {
        return orderDao.selectById(id);
    }

    @Override
    public boolean deleteById(int id) {
        return orderDao.deleteById(id) > 0;
    }

    @Override
    public List<Order> selectAllData() {
        return orderDao.selectAllData();
    }

    @Override
    public List<Order> selectAllPay(int uid) {
        return orderDao.selectAllPay(uid);
    }

    @Override
    public boolean updateStatus(int status, int id) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long times = System.currentTimeMillis();
        //return orderDao.updateStatus(status,id) > 0;
        return orderDao.updateStatusAndTime(status,id,format.format(times)) > 0;
    }

    @Override
    public List<Order> selectAllByUid(int uid) {
        return orderDao.selectByUid(uid);
    }

    @Override
    public boolean save(Order order) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long times = System.currentTimeMillis();
        order.setCreateTime(format.format(times));
        return orderDao.insert(order) > 0;
    }
}
